<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * ajax返回信息
 * @param string $msg 信息
 * @param array $redata 返回的数据，默认空数组
 * @param int $recode 错误码，1-未出现错误|其他出现错误
 * @param integer $code 状态码
 * @param array $header 头部
 * @param array $options 参数
 * @return \think\response\Json
 */
function ajax_return($msg = "", $redata = [], $recode = 1, $code = 200, $header = [], $options = [])
{
    $data = ["msg" => $msg, "data" => $redata, "code" => $recode];
    return think\Response::create($data, 'json', $code, $header, $options);
}

/**
 * ajax返回错误信息
 */
function ajax_return_error($msg = "", $redata = [], $recode = -1, $code = 200, $header = [], $options = [])
{
    return ajax_return($msg, $redata, $recode, $code, $header, $options);
}

/**
 * 记录日志
 * @param $file_name
 * @param $content
 * @param int $level_type
 * @return bool|int
 */
function write_log($file_name, $content, $level_type = 0)
{
    $level = array(
        0 => 'INFO',
        1 => 'DEBUG',
        2 => 'WARNING',
        3 => 'ERROR',
    );

    if (!isset($level[intval($level_type)])) {
        $level_type = 3;
    }

    $time = time();
    $file_path = \think\Config::get('log_path') . date('Y', $time) . '/' . date('m', $time) . '/' . date('d', $time);
    if (!file_exists($file_path)) {
        mkdir($file_path, 0777, true);
    }

    $content = '[' . date('Y-m-d H:i:s', $time) . '] ' . $level[$level_type] . ': ' . strval($content);
    return file_put_contents($file_path . '/' . strval($file_name) . '.log', $content . PHP_EOL, FILE_APPEND);
}